package com.taldh.mutlithread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * date 2020/7/9
 *
 * @author 袁小黑
 * @version 1.0.0
 **/

public class LockMemoryVars {

    static ReentrantLock lock = new ReentrantLock();
    static int count = 0;

    private static class Run1 implements Runnable {

        @Override
        public void run() {
            try {
                lock.lock();
                count++;
            } finally {
                lock.unlock();
            }
        }
    }
    private static class Run2 implements Runnable {

        @Override
        public void run() {
            try {
                lock.lock();
                System.out.println("count = " + count);
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(new Run1());
        executorService.execute(new Run2());
        executorService.shutdown();
    }

}
